home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr48 / pavt150.zip / CAPI.ZIP / JAMLREAD.C < prev    next >
C/C++ Source or Header  |  1993-07-01  |  3KB  |  126 lines

  1. /*
  2. **  JAM(mbp) - The Joaquim-Andrew-Mats Message Base Proposal
  3. **
  4. **  C API
  5. **
  6. **  Written by Joaquim Homrighausen.
  7. **
  8. **  ----------------------------------------------------------------------
  9. **
  10. **  jamlread.c (JAMmb)
  11. **
  12. **  LastRead handling
  13. **
  14. **  Copyright 1993 Joaquim Homrighausen, Andrew Milner, Mats Birch, and
  15. **  Mats Wallin. ALL RIGHTS RESERVED.
  16. **
  17. **  93-06-28    JoHo
  18. **  Initial coding.
  19. */
  20. #define JAMCAPI 1
  21.  
  22. #include "jammb.h"
  23.  
  24. /*
  25. **  Fetch LastRead for passed UserID. Returns 1 upon success and 0 upon
  26. **  failure.
  27. */
  28. int _JAMPROC JAMmbFetchLastRead(JAMAPIRECptr apirec, UINT32 UserID)
  29. {
  30.     INT32 ReadCount;
  31.     UINT32 LastReadRec;
  32.  
  33.     /* Make sure it's open */
  34.     if (!apirec->isOpen)
  35.         {
  36.         apirec->APImsg=JAMAPIMSG_ISNOTOPEN;
  37.         return (0);
  38.         }
  39.  
  40.     /* Seek to beginning of file */
  41.     if (apirec->SeekFunc(apirec, apirec->LrdHandle, JAMSEEK_SET, 0L)!=0L)
  42.         {
  43.         apirec->APImsg=JAMAPIMSG_SEEKERROR;
  44.         return (0);
  45.         }
  46.  
  47.     /* Read file from top to bottom */
  48.     LastReadRec=0L;
  49.     while (1)
  50.         {
  51.         ReadCount=apirec->ReadFunc(apirec, apirec->LrdHandle, &apirec->LastRead, (INT32)sizeof(JAMLREAD));
  52.         if (ReadCount!=(INT32)sizeof(JAMLREAD))
  53.             {
  54.             if (!ReadCount)
  55.                 /* End of file */
  56.                 apirec->APImsg=JAMAPIMSG_CANTFINDUSER;
  57.             else
  58.                 /* Read error */
  59.                 apirec->APImsg=JAMAPIMSG_CANTRDFILE;
  60.             return (0);
  61.             }
  62.  
  63.         /* See if it matches what we want */
  64.         if (apirec->LastRead.UserID==UserID)
  65.             {
  66.             apirec->LastLRDnum=LastReadRec;
  67.             apirec->APImsg=JAMAPIMSG_NOTHING;
  68.             return (1);
  69.             }
  70.  
  71.         /* Next record number */
  72.         LastReadRec++;
  73.         }/*while*/
  74.  
  75.     /* Dummy return to avoid warnings from some compilers */
  76.     return (0);
  77. }
  78.  
  79. /*
  80. **  Store LastRead record and if successful, optionally updates the header
  81. **  info block (and its ModCounter) at the beginning of the header file.
  82. **  Returns 1 upon success and 0 upon failure.
  83. */
  84. int _JAMPROC JAMmbStoreLastRead(JAMAPIRECptr apirec, int UpdateHdrInfo)
  85. {
  86.     INT32 UserOffset;
  87.  
  88.     /* Make sure it's open */
  89.     if (!apirec->isOpen)
  90.         {
  91.         apirec->APImsg=JAMAPIMSG_ISNOTOPEN;
  92.         return (0);
  93.         }
  94.  
  95.     /* Make sure it's locked */
  96.     if (!apirec->HaveLock)
  97.         {
  98.         apirec->APImsg=JAMAPIMSG_ISNOTLOCKED;
  99.         return (0);
  100.         }
  101.  
  102.     /* Seek to the appropriate position */
  103.     UserOffset=(INT32)(apirec->LastLRDnum * (INT32)sizeof(JAMLREAD));
  104.     if (apirec->SeekFunc(apirec, apirec->LrdHandle, JAMSEEK_SET, UserOffset)!=UserOffset)
  105.         {
  106.         apirec->APImsg=JAMAPIMSG_SEEKERROR;
  107.         return (0);
  108.         }
  109.  
  110.     /* Write record */
  111.     if (apirec->WriteFunc(apirec, apirec->LrdHandle, &apirec->LastRead, (INT32)sizeof(JAMLREAD))!=(INT32)sizeof(JAMLREAD))
  112.         {
  113.         apirec->APImsg=JAMAPIMSG_CANTWRFILE;
  114.         return (0);
  115.         }
  116.  
  117.     /* Update header info if told to */
  118.     if (UpdateHdrInfo && !JAMmbUpdateHeaderInfo(apirec, 1))
  119.         return (0);
  120.  
  121.     apirec->APImsg=JAMAPIMSG_NOTHING;
  122.     return (1);
  123. }
  124.  
  125. /* end of file "jamlread.c" */
  126.